<extend name="layout/edit" />

<block name="HEADER">

</block>

<block name="CONTENT">
    <control:form id="form"/>
</block>

<block name="FOOTER">
   <script type="text/javascript">
    var  page=(function(ns){
    	
		ns.init = function(){
		  
		}
		
		//选择父菜单
        ns.selectMenu=function(){
            function successFn(){
                window.tree = layui.tree.render({
                    elem: '#selectOrg' ,
                    textField:"nav_name",
                    idField:"nav_id",
                    parentField:'nav_pid',
                    data:'/admin/soft/nav/getTree',
                    isList:true,                         //是否列表结构数据，如果是，idField，parentField 必填
                    expandNode:0,                //可选，默认展开的节点，字符串
                    showIcon:function(e){     //显示图标，可为true
                        var icon = (e.icon || "").replace('&amp;','&');
                        return e.nav_pid == 0 ? (icon || "&#xe6a8;") :  (icon || '&#xe61f;');
                    },
                    loadSuccess:function(data){  //加载成功
                        ns.treeData = data;
                        data[0] && tree.selectNode("nav_id", Number(form.getValue("nav_pid")) );
                    }
                });
            }
            function yesFn(index){
                var node = tree.getSelectedNode(); 
                var nav_pname = $.map(ns.treeData, function(h) {
                	if(h.nav_id == node.nav_pid){
                	    return h.nav_name
                	}
                })[0];
                form.setValue("nav_pid",node.nav_id);
                form.setValue("nav_pname",(nav_pname ? (nav_pname+"/") : '')+node.nav_name);
                layer.close(index);
            }
            var html="<div id='selectOrg'></div>";
            $.openModalDialog(html,"选择父级",successFn,yesFn,["350px","370px"])
        }
		
		//保存
        ns.add=function(){
            form.save(4).then(function(res){
                $.ajaxs("/admin/soft/nav/insertAction",{
                    nav_id:res.data,
                    from_name: form.getValue("from_name"),
                    is_add_file: form.getValue("is_add_file")
                }).then(function(){
                    form.opener.$.toast(1,'添加成功！');
                    form.close();
                });
            });
        }
        
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



